package com.sap.health.tracker.library;

import android.R.array;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

public class StoreData extends Application{
		
		// Global locater for storing data
		private static final String PREFS_NAME = "UserPrefsFile";
		// select your mode to be either private or public.
		int mode= Activity.MODE_PRIVATE;
		
		// get the sharedPreference of your context.
		 SharedPreferences  userSharedPreferences; 
		
		/*
		 * save type string.
		 * var - name of the string
		 * content - what content should the string have
		 */
		public boolean saveType(Context ctx, Class<?> t, String varName, Object var){
			
			userSharedPreferences = ctx.getSharedPreferences(PREFS_NAME, mode);

			// retrieve an editor to modify the shared preferences
			SharedPreferences.Editor editor= userSharedPreferences.edit();
			
			if(t == String.class){ 
				
				   // now store your primitive type values. In this case it is true, 1f and Hello! World 
				   editor.putString(varName,(String)var);

				   //save the changes that you made
				   editor.commit(); 
				   
				   System.out.println("Saved Successfully");
				return true;
		    }
		    else if(t == int.class){ 
		    	editor.putInt(varName,(Integer) var);
		    	editor.commit(); 
		    	return true;
		    } else if(t == Boolean.class){ 	    	
		    	editor.putBoolean(varName,(Boolean) var);
		    	editor.commit(); 	  	
		    	return true;
		    } else if (t == array.class){
		    	
		    }
			//safe guard
		    return false;
		}
		
		public Object openType(Context ctx, Class<?> t, String varName){
			/* Now we will try to retrieve the data saved i.e. true, 1f and Hello! World */

			userSharedPreferences = ctx.getSharedPreferences(PREFS_NAME, mode);
						
			if(t == String.class){ 
				
				String mString= userSharedPreferences.getString(varName,"");
				
				return mString;
		    }
		    else if(t == int.class){ 
		    	int i = userSharedPreferences.getInt("cash", 0);		    	
		    	return i;
		    } else if (t == Boolean.class){
		    	Boolean bool = userSharedPreferences.getBoolean(varName, false);
		    	return bool;
		    }
			//safe guard
		    return null;
			
		}
		
		/*
		 * Removes specific shared preferences data.
		 */
		public boolean deleteType(Context ctx, String varName){
			userSharedPreferences = ctx.getSharedPreferences(PREFS_NAME, mode);
			System.out.println("id: "+ openType(ctx, String.class, "uid"));
			System.out.println("session: "+ openType(ctx, String.class, "session"));

			// retrieve an editor to modify the shared preferences
			SharedPreferences.Editor editor = userSharedPreferences.edit();			
			editor.remove(varName);
			if(editor.commit()){
				return true;
			}
			System.out.println("id: "+ openType(ctx, String.class, "uid"));

			System.out.println("session: "+ openType(ctx, String.class, "session"));
			return false;
		}
	   
}
